# -*- coding: utf-8 -*-
import json
import codecs
import MySQLdb
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html


class JianshuPipeline(object):
    def __init__(self):
        self.file = codecs.open("E:\jiaocheng\F\Project\\jianshu_article\data\\jianshu_uesr_info.json",'wb',encoding = 'utf-8')
        self.article_file = codecs.open("E:\jiaocheng\F\Project\\jianshu_article\data\\jianshu_article.json",'wb',encoding = 'utf-8')
        self.conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='jianshu',port=3306,charset = 'utf8')
        self.cur=self.conn.cursor() 
    def process_item(self, item, spider):
        if len(item) == 8 :
            dic = item
            dicts = json.dumps(dict(dic),ensure_ascii=False)
            line = dicts + '\n'
            self.file.write(line)
            try :
                value = [ item['name'],item['users_url'] , item['attention'] , item['fans'] ,\
                        item['article'] , item['words_num']  , item['gain_like'],item['intro'] ]
                self.cur.execute('insert into 简书网用户数据信息表(用户名,主页链接,关注的人数,粉丝数,\
                                    文章数,字数,收获喜欢,自我介绍)\
                                    values(%s,%s,%s,%s,%s,%s,%s,%s)',value)
                self.conn.commit()
                print "WRITE******mysql***INFO**SUCCESS"
            except MySQLdb.Error,e:
                print "Mysql Error %d: %s" % (e.args[0], e.args[1])
            return item
        elif len(item) == 12 :
            dic = {
                #文章标题
                "article_title" : item['article_title'][0],
                #作者
                "article_autho" : item['article_autho'][0],
                #最后编辑时间
                "article_edit_time" : item['article_edit_time'][0],
                #字数
                "article_num" : item['article_num'][0],
                #阅读数
                "article_read" : item['article_read'][0],
                #评论数
                "article_comment" : item['article_comment'][0],
                #喜欢数
                "article_like" : item['article_like'][0],
                #赞赏数
                "article_admire" : item['article_admire'][0],
                #作者信息：写了 **** 字，被 **** 人关注，获得了 **** 个喜欢
                #article_autho_info = scrapy.Field()
                #作者签名信息
                "article_autho_intro" : item['article_autho_intro'][0],
                #正文——文本——信息
                "article_txt" : item['article_txt'][0],
                #文章页面链接
                "article_url" : item['article_url'][0],
                #微博长图链接
                "weibo_image" : item['weibo_image'][0],
                  }
            dicts = json.dumps(dict(dic),ensure_ascii=False)
            line = dicts + '\n'
            self.article_file.write(line)
            try:
                value = [dic['article_title'],dic['article_url'],dic['article_autho'],dic['article_edit_time'],\
                        dic['article_num'] , dic['article_read']  , dic['article_comment'],dic['article_like'],\
                        dic['article_admire'],dic['article_autho_intro'],dic['weibo_image'],dic['article_txt'] ]
                self.cur.execute('insert into 简书文章表(\
                                    文章标题,文章链接,文章作者,最后编辑时间,\
                                    文章字数,文章阅读数,文章评论数,文章喜欢人数,\
                                    文章赞赏次数,文章作者自我介绍,微博长图链接,文章正文)\
                                    values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',value)
                self.conn.commit()
                print "Title:%s*****URL:%s\n"%(dic['article_title'],dic['article_url'])
                print "\nWRITE******mysql***ARTICLE**SUCCESS\n"
            except MySQLdb.Error,e:
                print "Mysql Error %d: %s" % (e.args[0], e.args[1])

            return item
    def close_spider(self,spider):
        self.file.close()
        self.article_file.close()
